﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessObject;
using Utility;
using System.Xml;
using System.Web.Security;
using BusinessLogic;
namespace WebTeamplate
{
    public partial class Admin_UserControl_ucMainMenu : BaseUserControl
    {
        #region Fields
        public string menuParentName = string.Empty;
        public string menuChildName = string.Empty;
        public List<MenuResult> lstMenu = new List<MenuResult>();
        public List<MenuResult> lstMenuChild = new List<MenuResult>();
        public List<MenuResult> lstMenuChildLevel2 = new List<MenuResult>();
        #endregion Fields

        #region ViewState

        /// <summary>
        /// Define variable type of ViewState hold MenuID
        /// </summary>
        protected int MenuID
        {
            get
            {

                object o = ViewState["MenuID"];
                if (o == null)
                {
                    return int.MinValue;
                }
                return (Int32)o;
            }
            set
            {
                ViewState["MenuID"] = value;
            }
        }
        /// <summary>
        /// Define variable type of ViewState hold MenuID
        /// </summary>
        protected int SubMenuID
        {
            get
            {

                object o = ViewState["SubMenuID"];
                if (o == null)
                {
                    return int.MinValue;
                }
                return (Int32)o;
            }
            set
            {
                ViewState["SubMenuID"] = value;
            }
        }
        #endregion

        #region Init,load
        protected void Page_Load(object sender, EventArgs e)
        {
           
                LoadListMenuForAdmin();
          
            
        }
        #endregion

        #region Method
       
        
        public List<MenuResult> GetListChild(MenuResult objParent)
        {
            List<MenuResult> lstResult = new List<MenuResult>();
            foreach (MenuResult objMenu in lstMenu)
            {
                if (objParent.Id == objMenu.ParentID)
                {
                    lstResult.Add(objMenu);
                }
            }
            return lstResult;
        }
     
        /// <summary>
        /// Load DataSource for list menu
        /// </summary>
        /// <returns></returns>
        private void LoadListMenuForAdmin()
        {
            string page = GetCurrentPage();                       
            try
            {
                Vw_aspnet_RolesResult objRole = UserProfileUtil.GetMaxRole(this.UserloginId, this.ApplicationName);
                if (objRole == null)
                    return;
                XmlDocument xmlDoc = new XmlDocument();
                string file = Server.MapPath("~/Admin/UserControl/" + objRole.MenuFile);
                xmlDoc.Load(file);
                MenuResult objParent;
                MenuResult objChild;
                int parentMenu = GetMenuParentIDByLink(xmlDoc, page);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    objParent= ParseMenu(node);
                    if (objParent.IsActive == BaseFunctions.TRUE && objParent.ParentID < 0)
                    {                        
                        foreach (XmlNode subMenu in node.ChildNodes)
                        {
                            objChild = ParseMenu(subMenu);
                            if (objChild.IsActive == BaseFunctions.TRUE && objChild.ParentID == objParent.Id)
                            {
                                lstMenu.Add(objChild);//add menu child
                                objParent.HasChild = true;
                                if (objChild.ParentID == parentMenu)
                                {
                                    lstMenuChildLevel2.Add(objChild);
                                    if (objChild.Link == page)
                                    {
                                        menuChildName = objChild.Name;
                                        menuParentName = objParent.Name;
                                    }
                                }
                            }
                            else
                            {
                                if (objChild.IsActive == BaseFunctions.FALSE && objChild.ParentID == objParent.Id)
                                {                                  
                                    if (objChild.ParentID == parentMenu)
                                    {
                                 
                                        if (objChild.Link == page)
                                        {
                                            menuChildName = objChild.Name;
                                            menuParentName = objParent.Name;
                                        }
                                    }
                                }
                            }
                        }
                        lstMenu.Add(objParent);//add parent
                    }
                }
            }
            catch
            {
            }
          
        }
        private void LoadListMenuForSupperAdmin()
        {
            string page = GetCurrentPage();
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                string file = Server.MapPath("~/Admin/UserControl/SupperAdmin.xml");
                xmlDoc.Load(file);
                MenuResult objParent;
                MenuResult objChild;
                int parentMenu = GetMenuParentIDByLink(xmlDoc, page);
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    objParent = ParseMenu(node);
                    if (objParent.IsActive == BaseFunctions.TRUE && objParent.ParentID < 0)
                    {
                        foreach (XmlNode subMenu in node.ChildNodes)
                        {
                            objChild = ParseMenu(subMenu);
                            if (objChild.IsActive == BaseFunctions.TRUE && objChild.ParentID == objParent.Id)
                            {
                                lstMenu.Add(objChild);//add menu child
                                objParent.HasChild = true;
                                if (objChild.ParentID == parentMenu)
                                {
                                    lstMenuChildLevel2.Add(objChild);
                                    if (objChild.Link == page)
                                    {
                                        menuChildName = objChild.Name;
                                        menuParentName = objParent.Name;
                                    }
                                }
                            }
                        }
                        lstMenu.Add(objParent);//add parent
                    }
                }
            }
            catch
            {
            }

        }
        private int GetMenuParentIDByLink(XmlDocument xmlDoc, string link)
        {
            MenuResult objParent;
            MenuResult objChild;
            foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
            {
                objParent = ParseMenu(node);
                if (objParent.IsActive == BaseFunctions.TRUE && objParent.ParentID < 0)
                {
                    foreach (XmlNode subMenu in node.ChildNodes)
                    {
                        objChild = ParseMenu(subMenu);
                        if (objChild.Link.Trim() == link.Trim())
                            return objChild.ParentID;
                    }                    
                }
            }
            return 0;
        }
        private string GetCurrentPage()
        {

            string sPath = System.Web.HttpContext.Current.Request.Url.AbsolutePath;
            sPath = sPath.Substring(1, sPath.Length - 1);
            int index = sPath.IndexOf('/');
            if (index > 0)
                sPath = sPath.Substring(index + 1, sPath.Length - (index + 1));
            return sPath;
        }
        private MenuResult ParseMenu(XmlNode node)
        {
            MenuResult result = new MenuResult();
            result.Id = ConvertValueObject.ParseInt(node.Attributes["id"].Value.ToString());
            result.Link = node.Attributes["link"].Value.ToString();
            result.Name = node.Attributes["name"].Value.ToString();
            result.ParentID = ConvertValueObject.ParseInt(node.Attributes["parent"].Value.ToString());
            result.IsActive = node.Attributes["isActive"].Value.ToString();
            return result;
        }
        public bool IsAdminDefaultPage()
        {
            if (System.Web.HttpContext.Current.Request.Url.AbsolutePath.Contains("Admin/Default.aspx"))
                return true;
            return false;
        }
        #endregion

    }
}
